package com.yfj.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yfj.entity.Course;
import com.yfj.service.ManagerService;

@Controller
public class ManagerController {
	
	@Autowired
	ManagerService managerService;

//addCourse页面-----------------------------------------------------------------------------------------------------

	@RequiresRoles("manager")
	@RequestMapping({"/manager","/showAddCourse"})
	public String showAddCourse() {
		return "manager/addCourse";
	}

	@RequiresRoles("manager")
	@RequiresPermissions("course:add")
	@RequestMapping("/addCourse")
	@ResponseBody
	public int addCourse(Course c) {
		System.out.println(c.toString());
		int mark=0;		//添加成功的标志,默认置0
		try {
			managerService.addCourse(c);
			mark=1;		//添加语句执行成功未抛出异常,置为1
		}catch(Exception e){
			mark=0;
		}
		return mark;
	}
	
//allCourse页面-----------------------------------------------------------------------------------------------------
	@RequiresRoles("manager")
	@RequestMapping("/showAllCourse")
	public String showAllCourse() {
		return "manager/allCourse";
	}
	
	/*
	 * 查询或显示全部
	 */
	@RequiresPermissions("course:queryAll")
	@RequestMapping({"/allCourse","/queryCourse"})
	@ResponseBody
	public Map<String,Object> queryCourse(String name,String college,String major,String teacherName,Integer currPage){
	    Map<String,Object> map=new HashMap<String,Object>();
		Map<String,Object> m=new HashMap<String,Object>();          //查询条件
		m.put("name", name);
		m.put("college", college);
		m.put("major", major);
		m.put("teacherName",teacherName);
        PageHelper.startPage(currPage,9);      //当前页,查询和初始加载都为1,翻页操作会加载指定页
		List<Course> cList=managerService.queryCourse(m);
		PageInfo pageInfo=new PageInfo(cList);
		long total=pageInfo.getTotal();
		map.put("cList",cList);                          //要显示的课程列表
		map.put("total",total);                          //总数据数目
		return map;
	}

	/*
	 * 修改状态
	 */
	@RequiresRoles("manager")
	@RequiresPermissions("course:changeState")
	@RequestMapping("/changeState")
	@ResponseBody
	public int changeState(Long id,String state) {
		System.out.println("####");
		System.out.println("####id:"+id+"  state:"+state);
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("id",	id);
		m.put("state", state);
		int mark=0;
		try{
			managerService.changeState(m);
			mark=1;
		}catch (Exception e){}
		return mark;
	}
	
	/**
	 * 开放所选
	 */
	@RequiresRoles("manager")
	@RequiresPermissions("course:openCourse")
	@RequestMapping("/openCourseList")
	@ResponseBody
	public int openCourseList(Long[] ids) {
	    int mark=0;
	    try{
            for (Long id : ids) {
                Map<String,Object> m=new HashMap<String,Object>();
                m.put("id",	id);
                m.put("state", "true");
                managerService.changeState(m);
            }
            mark=1;
        }catch (Exception e){

        }
		return mark;
	}
	
	/**
	 * 删除所选
	 */
	@RequiresRoles("manager")
	@RequiresPermissions("course:delete")
	@RequestMapping("/deleteCourseList")
	@ResponseBody
	public int  deleteCourseList(Long[] ids) {
		Map<String,Object> map=new HashMap<String,Object>();
		int mark=0;
		try {
			for (Long id : ids) {
				managerService.deleteCourse(id);
			}
			mark=1;
		}catch(Exception e) {}
		return mark;
	}
}
